package com.example.work_cprovider2;

import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddActivity extends AppCompatActivity {
    private EditText munber;
    private EditText name;
    private EditText age;
    private Button result;
    private LocalBroadcastManager localBroadcastManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        munber = (EditText)findViewById(R.id.e_munber);
        name = (EditText)findViewById(R.id.e_name);
        age = (EditText)findViewById(R.id.e_age);
        result = (Button)findViewById(R.id.e_result);
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        result.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!munber.getText().toString().equals("") && !name.getText().toString().equals("")&&!age.getText().toString().equals("")){
                    int m =Integer.parseInt( munber.getText().toString());
                    String n = name.getText().toString();
                    int a = Integer.parseInt(age.getText().toString());
                    Uri uri = Uri.parse("content://com.example.databasetext.provider/student");
                    ContentValues values = new ContentValues();
                    values.put("munber",m);
                    values.put("name",n);
                    values.put("age",a);
                    getContentResolver().insert(uri,values);
                    Intent intent = new Intent("com.example.broadcast.ADD");
                    localBroadcastManager.sendBroadcast(intent);
                    finish();
                }else{
                    Toast.makeText(AddActivity.this,"资料不全，无法添加",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
